package com.eascs.admin.o2o.recommend.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.internal.util.BaseUserUtil;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.recommend.dao.O2oRecommendDao;
import com.eascs.admin.o2o.recommend.entity.O2oRecommendEntity;
import com.eascs.admin.o2o.recommend.vo.O2oRecommendVo;
import com.eascs.app.dao.PageResult;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Service
public class O2oRecommendService {
	
	@Autowired
	O2oRecommendDao o2oRecommendDao;
	

	@Autowired
	BaseUserUtil userUtils;
	
	@Autowired
	private O2oZxServiceImpl o2oZxServiceImpl;
	
	@Reference
	SysOrgService sysOrgService;
	
	/**
	 * @Description:
	 *               <p>
	 *               代偿结清查询
	 *               </p>
	 * @author CXW
	 * @date 2017年4月18日 下午1:38:16
	 * @version V1.0
	 *//*
	public PageResult<Object[]> getSettlePage(O2oSettleVo vo){
		return O2oRecommendDao.getSettlePage(vo);
	}*/
	/*
	*//**
	 * @Description:
	 *               <p>
	 *               根据EA放款表id或者代偿结清id查询代偿结清信息
	 *               </p>
	 * @author CXW
	 * @date 2017年4月18日 下午4:09:13
	 * @version V1.0
	 *//*
	public O2oRecommendEntity findEntity(O2oSettleVo vo){
		return O2oRecommendDao.findEntity(vo);
	}*/
	
	/**
	 * @Description:
	 *               <p>
	 *               根据代偿结清id查询代偿结清信息
	 *               </p>
	 * @author CXW
	 * @date 2017年4月18日 下午4:10:57
	 * @version V1.0
	 */
	public O2oRecommendEntity getById(String id){
		return o2oRecommendDao.getById(O2oRecommendEntity.class, id);
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               根据代偿结清id更新代偿结清表信息
	 *               </p>
	 * @author CXW
	 * @date 2017年4月18日 下午4:12:08
	 * @version V1.0
	 */
	@Transactional(transactionManager = "o2oTransactionManager")
	public int updateById(O2oRecommendEntity entity){
		return o2oRecommendDao.updateById(entity);
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               插入数据表
	 *               </p>
	 * @author CXW
	 * @date 2017年4月18日 下午4:21:43
	 * @version V1.0
	 */
	@Transactional(transactionManager = "o2oTransactionManager")
	public int insertEntity(O2oRecommendEntity entity){
		return o2oRecommendDao.insert(entity);
	}
	
	/**
	 * 页面传的对象处理存库 save or update
	 * @param o2oRecommendEntity
	 * @return
	 */
	
	public int saveorupdateEntity(O2oRecommendEntity o2oRecommendEntity){
		
		User user = UserHolder.getUser();
		//if(!"1".equals(user.getOrg()) && o2oRecommendEntity.getId()!=null){ //编辑 总部看所有，总部不更新
			if( StatusContexts.RECOMMENDSQ_TYPE_O.equals(o2oRecommendEntity.getSqType())){
				o2oRecommendEntity.setOperatorNo(user.getNo());
				o2oRecommendEntity.setOperatorName(user.getCname());
				o2oRecommendEntity.setOperatorProvinceId(user.getOrg());
				ActOrgEntity  actOrgEntity = sysOrgService.getOrgByIds(user.getOrg());
				o2oRecommendEntity.setOperatorProvinceName(actOrgEntity.getOrgName());
			}else{
				o2oRecommendEntity.setReferrerNo(user.getNo());
				o2oRecommendEntity.setReferrerName(user.getCname());
				o2oRecommendEntity.setReferrerProvinceId(user.getOrg());
				ActOrgEntity  actOrgEntity = sysOrgService.getOrgByIds(user.getOrg());
				o2oRecommendEntity.setReferrerProvinceName(actOrgEntity.getOrgName());
			}
		//}
		
		ActOrgEntity  actOrgEntity = sysOrgService.getOrgByIds(o2oRecommendEntity.getOperatorProvinceId());
		if(actOrgEntity!=null){
			o2oRecommendEntity.setOperatorProvinceName(actOrgEntity.getOrgName());
		}
		
		ActOrgEntity  actOrgEntity1 = sysOrgService.getOrgByIds(o2oRecommendEntity.getReferrerProvinceId());
		if(actOrgEntity1!=null){
			o2oRecommendEntity.setReferrerProvinceName(actOrgEntity1.getOrgName());
		}
		
		if(o2oRecommendEntity.getId()==null){//新增
			o2oRecommendEntity.setCreatorNo(userUtils.getUser().getNo());
			o2oRecommendEntity.setCreatorTime(new Date());
			o2oRecommendEntity.setCreatorName(userUtils.getUser().getCname());
			o2oRecommendEntity.setIsType(StatusContexts.O2O_YES);
			o2oRecommendEntity.setBillStatus(StatusContexts.O2O_NEW);
			o2oRecommendEntity.setBillCode(o2oZxServiceImpl.buildCode("REC"));
			return o2oRecommendDao.insert(o2oRecommendEntity);
		}else{
			O2oRecommendEntity o2oRecommendEntityD  = getById(o2oRecommendEntity.getId());
			o2oRecommendEntityD.setSqType(o2oRecommendEntity.getSqType());
			o2oRecommendEntityD.setUserName(o2oRecommendEntity.getUserName());
			o2oRecommendEntityD.setUserPhone(o2oRecommendEntity.getUserPhone());
			o2oRecommendEntityD.setUserPhone(o2oRecommendEntity.getUserPhone());
			if( StatusContexts.RECOMMENDSQ_TYPE_O.equals(o2oRecommendEntity.getSqType())){
				o2oRecommendEntityD.setOperatorNo(user.getNo());
				o2oRecommendEntityD.setOperatorName(user.getCname());
				o2oRecommendEntityD.setOperatorProvinceId(user.getOrg());
				ActOrgEntity  actOrgEntity2 = sysOrgService.getOrgByIds(user.getOrg());
				o2oRecommendEntityD.setOperatorProvinceName(actOrgEntity2.getOrgName());
			}else{
				o2oRecommendEntityD.setReferrerNo(user.getNo());
				o2oRecommendEntityD.setReferrerName(user.getCname());
				o2oRecommendEntityD.setReferrerProvinceId(user.getOrg());
				ActOrgEntity  actOrgEntity2 = sysOrgService.getOrgByIds(user.getOrg());
				o2oRecommendEntityD.setReferrerProvinceName(actOrgEntity2.getOrgName());
			}
			return o2oRecommendDao.updateById(o2oRecommendEntityD);
		}
		
		
	}
	

	public PageResult<Object[]>  findByPage(O2oRecommendVo entity){
		
		return o2oRecommendDao.findByPage(entity);
	}
	
	
	/**
	 * @Description:
	 *               <p>
	 *              根据身份证查询客户推荐表中是否有推荐人
	 *               </p>
	 * @author CXW
	 * @date 2018年2月27日 下午1:37:43
	 * @version V1.0
	 */
	public O2oRecommendEntity findEntityByCard(String documentNo) {
		String count_sql = "select * from o2o_recommend orr where orr.document_no=? and orr.share_start_date<=now() and orr.share_end_date>=now() and orr.is_type='Y' ";
		return o2oRecommendDao.findEntity(count_sql, O2oRecommendEntity.class, documentNo);
	}
	
	
	
}
